﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Wuqi.Webdiyer;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web
{
    public partial class buyingLeads : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var key = Request.QueryString["key"];
                try
                {
                    BindNews();
                    if (string.IsNullOrEmpty(key))
                    {
                        AspNetPager1.RecordCount = NewsInfoBll.GetNewsInfoCount("NC_Code=001 and News_Status=1",
                                                                                CommonConst.DB_Name);
                    }
                    else
                    {
                        //插入关键字
                        string ip = HttpContext.Current.Application["Computerip"].ToString();
                        ProductInfoBll.InsertSearchKey(Server.UrlDecode(key.Substring(1)), ip, 1, CommonConst.DB_Name);
                        AspNetPager1.RecordCount = NewsInfoBll.GetNewsInfoCount("NC_Code=001 and News_Status=1 and News_Title like '%" + key.Substring(1) + "%' ",
                                                                                CommonConst.DB_Name);
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("/shop/error.html");
                }
            }
        }

        private void BindNews()
        {
            //热门信息
            var dicHot = new Dictionary<string, object>();
            dicHot.Add("NC_Code", "001");
            dicHot.Add("News_Status", 1);
            var dtHot = NewsInfoBll.GetNewsInfoTable("top 10 *", dicHot, "News_Times,", CommonConst.DB_Name,true);
            repHot.DataSource = dtHot;
            repHot.DataBind();
            //推荐信息
            dicHot.Add("more", "CHARINDEX('c',News_Flag)<>0");
            var dtRec = NewsInfoBll.GetNewsInfoTable("top 10 *", dicHot, "News_Times,", CommonConst.DB_Name,false);
            repRec.DataSource = dtRec;
            repRec.DataBind();
        }

        //绑定求购信息
        private void BindAllNews()
        {
            var key = Request.QueryString["key"];
            var dicHot = new Dictionary<string, object>();
            dicHot.Add("NC_Code", "001");
            dicHot.Add("News_Status", 1);
            if (!string.IsNullOrEmpty(key))
            {
                dicHot.Add("more", " News_Title like '%" + key.Substring(1) + "%' ");
            }
            var pageIndex = AspNetPager1.CurrentPageIndex-1;
            var pageSize = AspNetPager1.PageSize;
            string orderBy = "case when Vip_EndDate is null or Vip_EndDate<getdate() then '1900-1-1' end,User_Points, News_Date desc";
            var dt = NewsInfoBll.GetNewsInfoTable(pageIndex, pageSize, "*", orderBy, dicHot, CommonConst.DB_Name);
            repNews.DataSource = dt;
            repNews.DataBind();
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            BindAllNews();
        }
    }
}
